package net.hust.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.hust.bean.Comment;
import net.hust.service.CommentService;

@Service@Transactional
public class CommentServiceBean implements CommentService{

	@Resource SessionFactory factory;
	public void delete(int commentId) {
		factory.getCurrentSession().delete(factory.getCurrentSession().load(Comment.class, commentId));
		
	}

	public void delete(Integer... commetIds) {
		for(Integer commentId : commetIds){
			factory.getCurrentSession().delete(factory.getCurrentSession().load(Comment.class, commentId));
		}
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Comment find(Integer commetId) {
		Comment comment = (Comment)factory.getCurrentSession().get(Comment.class,commetId);
		return comment;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Comment> list(String songname) {
		String sql = "from Comment as a where song_name='" + songname + "'";
		List<Comment> comments = factory.getCurrentSession().createQuery(sql).list();
//		System.out.println(""+comments.get(0).getComments());
		return comments;
	}

	public void save(Comment comment) {
		factory.getCurrentSession().persist(comment);
	}

	public void update(Comment comment) {
		factory.getCurrentSession().merge(comment);
	}

}
